<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @author duongbq
 * Date 2011 - 05 -11
 */
class Ticket_Tag_Model extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }
    
    private function _set_where($params = array()) {
        if (isset($params['ticket_id']))
            $this->db->where('ticket_tag.ticket_id', $params['ticket_id']);

        if (isset($params['tag_id']) && $params['tag_id'] != -1)
            $this->db->where('ticket_tag.tag_id', $params['tag_id']);

        if (isset($params['tag_name']))
            $this->db->where('tag.tag_name', $params['tag_name']);
    }
    
    /**
     * @author duongbq
     * @param type $params
     * @return type 
     */
    function get_ticket_tag($params = array()) {
        $this->_set_where($params);

        $this->db->select('ticket_tag.*, tag.tag_name');

        $this->db->join('tag', 'tag.id = ticket_tag.tag_id');

        return $this->db->get('ticket_tag')->result();
    }
    
    function add_ticket_tag($data = array()) {
        $this->db->insert('ticket_tag', $data);
        return $this->db->insert_id(); 
    }
    
    
    
    
    function get_ticket_tag_array($params = array())
    {
        $tasks_tags = $this->get_ticket_tag($params);
        $output = array();
        foreach($tasks_tags as $item)
        {
            $output[$item->tag_id] = $item->ticket_id;
        }
        return $output;
    }
    
    
    
    function delete_ticket_tag($data = array()) {
        
        if (isset($data['ticket_id']))
            $this->db->where('ticket_tag.ticket_id', $data['ticket_id']);

        if (isset($data['tag_id']))
            $this->db->where('ticket_tag.tag_id', $data['tag_id']);

        return $this->db->delete('ticket_tag');
    }
    
}
